﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageInventory
{
    public partial class EditItem : Form
    {
        public EditItem()
        {
            InitializeComponent();
        }

        public bool addingItem = false;
        public string averageProfit;

        private void EditItem_Load(object sender, EventArgs e)
        {
            if (addingItem == false) loadInfo();
            else
            {
                groupBox1.Text = "";
                panel1.Visible = false;
                radioButton2.Enabled = false;
                radioButton1.Checked = true;
            }

            //set average profit trackbar and label
            label16.Text = averageProfit;
            trackBar2.Value = System.Convert.ToInt32(averageProfit.Replace("%", "").Split('.')[0]);
        }

        #region Transfering Item Info

        void loadInfo()
        {
            groupBox1.Text = itemInfo[0];
            textBox1.Text = itemInfo[0];
            textBox4.Text = itemInfo[3];
            label1.Text = itemInfo[1];
            label6.Text = itemInfo[2];
            label7.Text = itemInfo[3];
            label8.Text = itemInfo[4];
        }

        public string[] itemInfo;

        public string[] setInfo
        {
            get
            {
                string[] tmp = new string[5];
                tmp[0] = groupBox1.Text;
                tmp[1] = label1.Text;
                tmp[2] = label6.Text;
                tmp[3] = label7.Text;
                tmp[4] = label8.Text; 
                return tmp;
            }
        }


        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (addingItem == false)
            {
                if (textBox1.Text != "") groupBox1.Text = textBox1.Text;

                if (textBox2.Text != "")
                {

                    int var = System.Convert.ToInt32(textBox2.Text);
                    int qty = System.Convert.ToInt32(label6.Text);
                    if (radioButton1.Checked) label6.Text = (qty + var).ToString();
                    else if (radioButton2.Checked)
                    {
                        if (System.Convert.ToInt32(textBox2.Text) < System.Convert.ToInt32(label6.Text))
                        {
                            label6.Text = (qty - var).ToString();
                        }
                        else MessageBox.Show("Not enough in stcok to decrease that amount of quantity. Saving other changes..");
                    }
                }

                if (textBox4.Text != "")
                {
                    label7.Text = textBox4.Text;
           
                }

                if (textBox5.Text != "")
                {
                    label8.Text = textBox5.Text;
                }

                this.DialogResult = DialogResult.OK;
            }
            else
            {
                if (textBox1.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
                {
                    MessageBox.Show("Missing values.");

                }
                else
                {
                    this.DialogResult = DialogResult.Yes;
                }
            }
        }

        public string getInfo
        {
            get
            { return textBox1.Text + "," + textBox6.Text + "," + textBox2.Text + "," + textBox4.Text + "," + textBox5.Text; }
        }


        #endregion

        #region Profit Calculations

        bool enableUpdatingByTrack = false;
        bool enableUpdatingByPercent = false;

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (enableUpdatingByPercent == true)
                {
                    trackBar1.Value = System.Convert.ToInt32(System.Convert.ToDouble(textBox3.Text));
                    updateProfitByTrack();
                }
            }
            catch { }
            textBox3.SelectionStart = textBox3.Text.Length;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (enableUpdatingByTrack == true)
            {
                updateProfitByTrack();
            }
        }

        void updateProfitByTrack()
        {
            try
            {
                textBox3.Text = trackBar1.Value.ToString();
                textBox5.Text = (Math.Round((System.Convert.ToDouble(textBox4.Text) * (trackBar1.Value / 100d)) + System.Convert.ToDouble(textBox4.Text), 2)).ToString();
            }
            catch { }
        }

        void updateProfitGeneric()
        {
            double percent;
            try
            {
                percent = ((System.Convert.ToDouble(textBox5.Text) - System.Convert.ToDouble(textBox4.Text)) / System.Convert.ToDouble(textBox4.Text)) * 100d;
                textBox3.Text = Math.Round(percent, 2).ToString();
                trackBar1.Value = System.Convert.ToInt32(percent);
            }
            catch
            {
                //percent = 0;
            }
            try
            {
                
                
            }
            catch { }
        }

        private void trackBar1_MouseDown(object sender, MouseEventArgs e)
        {
            enableUpdatingByTrack = true;
        }

        private void trackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            enableUpdatingByTrack = false;
        }

        private void textBox3_Enter(object sender, EventArgs e)
        {
            enableUpdatingByPercent = true;
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            enableUpdatingByPercent = false;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

            updateProfitGeneric();

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            updateProfitGeneric();
        }

        #endregion

        #region Original Description
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text == groupBox1.Text)
            {
                textBox1.SelectionStart = 0;
                textBox1.SelectionLength = textBox1.Text.Length;
            }

        }
        #endregion

        #region Exit
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        #endregion

        private void textBoxInt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void textBoxDecimal_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }


        public void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            numwithdecitxtBoxx_KeyPress(sender, e, textBox4);
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            numwithdecitxtBoxx_KeyPress(sender, e, textBox5);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            numwithdecitxtBoxx_KeyPress(sender, e, textBox3);
        }

        void numwithdecitxtBoxx_KeyPress(object sender, KeyPressEventArgs e, TextBox tb)
        {
            if (char.IsDigit(e.KeyChar))
            {
                int len = tb.Text.Length;
                int pos = tb.Text.IndexOf('.');
                int index = tb.SelectionStart;
                if (pos == 0 && len == 3 && index > pos) e.Handled = true;
                if (pos > 0)
                {
                    index = tb.SelectionStart;
                    if (len - pos == 3)
                    {
                        if (index > pos) e.Handled = true;
                    }
                }
            }
            else if (e.KeyChar == '.')
            {
                if (tb.Text.IndexOf('.') != -1) e.Handled = true;
            }
            else if (e.KeyChar != '\b') e.Handled = true;
        }
    }
}
